****************************************************
*Risk factor for different methods
****************************************************

/*
*If run as standalone, wouold have to create a dataseries of the desired size
set more off, perm
clear
graph drop _all
local dir "/Users/rreis/Dropbox/05Shared_folders/HRR/HRR_InflDisaster/Ricardo"  // RR's directory
*local dir "C:\Users\marin\Dropbox\HRR_InflDisaster\Ricardo"  // Marina's directory
cd `dir'
*/

*****************************
* Set key parameters
*pick method
scalar method	=	6   	// pick method 2.3 from R files
scalar gamma	=	3   	// risk aversion coefficient

*unconditions probability of output disaster
scalar pyd		= 0.013		// probability of output disaster from Barro, R file too
scalar pyd = (1- (1-pyd)^5) // 5-year terms

* 2) Run the newly created pareto_matrix.do
do pareto_matrix.do


*****************************
*Risk factors for pooled sample
*  Read off fror R printout for 6 columns of methods: punc, pcond, z0, alpha
*	matrix Pareto = (0.213,0.207,0.132,0.206,0.213,0.134 ///
*				\ 0.167,0.18,0.213,0.169,0.186,0.200 ///
*				\ 1.04,1.03,1.04,1.03,1.03,1.03 ///
*				\ 5.73,5.7,6.77,6.11,6.67,6.38)
* rownames Pareto = Prob_pid_unc Prob_yd_cond_pid z0 alpha
*matrix list Pareto

*  Calculate adjustments using formula
scalar z0 = Pareto[3,method] 
scalar alpha = Pareto[4,method]
g md = (alpha/(alpha-gamma))*z0^gamma  //formula for m_d
g ptilde = Pareto[2,method]   		   // prob (y_d / pi_d)
g RiskFactor = 1/(1 + ptilde*(md-1))   //formula from paper
label variable RiskFactor "Risk factor, pooled constant"
summ RiskFactor ptilde md    		   // reads to be 0.82

*  Calculate time varying one using second formula -- this actually did not end up getting used
* g pinvtilde =  (Pareto[1,method] / pyd )*ptilde // this is prob(pi_d/y_d)

*clean repeated scalars
scalar drop z0 alpha 

*****************************
*Risk factors for high inflation sample
*  Read off fror R printout for 6 columns of methods: punc, pcond, z0, alpha
matrix Paretopos = (0.133,0.129,0.063,0.124,0.131,0.057 ///
				\ 0.203,0.23,0.303,0.227,0.251,0.356 ///
				\ 1.07, 1.03, 1.05, 1.03, 1.03, 1.03 ///
				\ 5.4, 5.11, 5.73, 5.4, 6.09, 5.45)
matrix rownames Paretopos = Prob_pid_unc Prob_yd_cond_pid z0 alpha
matrix list Paretopos

*calculate adjustments using formula
scalar z0 = Paretopos[3,method] 
scalar alpha = Paretopos[4,method]
g md_pos = (alpha/(alpha-gamma))*z0^gamma
g ptilde_pos = Paretopos[2,method]
g RiskFactor_pos = 1/(1 + ptilde_pos*(md_pos-1))
summ RiskFactor_pos ptilde_pos md_pos  // reads to be 0.65
label variable RiskFactor_pos "Risk factor, high-inflation constant"

*  Calculate time varying one using second formula
* g pinvtilde_pos =  (Paretopos[1,method] / pyd )*ptilde_pos // this is prob(pi_d/y_d)

*clean repeated scalars
scalar drop z0 alpha 

*****************************
*Risk factors for deflation sample
*  Read off fror R printout for 6 columns of methods: punc, pcond, z0, alpha
matrix Paretoneg = (0.105,0.102,0.078,0.105,0.108,0.086 ///
				\ 0.121,0.114,0.142,0.085,0.085,0.085 ///
				\ 1.04, 1.03, 1.04, 1.06, 1.06, 1.06 ///
				\ 10.84, 8.62, 11.55, 15.18, 15.18, 15.18)
matrix rownames Paretoneg = Prob_pid_unc Prob_yd_cond_pid z0 alpha
matrix list Paretoneg

*calculate adjustments using formula
scalar alpha = Paretoneg[4,method]
scalar z0 = Paretoneg[3,method] 
g md_neg = (alpha/(alpha-gamma))*z0^gamma
g ptilde_neg = Paretoneg[2,method]
g RiskFactor_neg = 1/(1 + ptilde_neg*(md_neg-1))
summ RiskFactor_neg ptilde_neg md_neg  // reads to be 0.96
label variable RiskFactor_neg "Risk factor, deflation constant"

*  Calculate time varying one using second formula
* g pinvtilde_neg =  (Paretoneg[1,method] / pyd )*ptilde_neg // this is prob(pi_d/y_d)

*clean repeated scalars
scalar drop z0 alpha 

/*. 
*Run this bit only if using as standalone
sleep 100
save RiskAdjustments, replace
